package com.joker.aigc.tools.controller;

import com.joker.aigc.domain.entity.R;
import com.joker.aigc.tools.domian.dto.VideoDetailDTO;
import com.joker.aigc.tools.domian.dto.VideoListDTO;
import com.joker.aigc.tools.service.video.VideoService;
import com.joker.aigc.utils.RequestHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 视频控制器
 * @author system
 * @date 2023-12-01
 */
@Tag(name = "视频管理", description = "视频上传、查询、播放等功能")
@Validated
@RestController
@RequestMapping("/api/videos")
public class VideoController {
    
    @Resource
    private VideoService videoService;


    
    /**
     * 获取视频详情
     * @param videoId 视频ID
     * @return 视频详情
     */
    @Operation(
        summary = "获取视频详情",
        description = "根据视频ID获取视频的详细信息，包括分类、演员、用户数据等"
    )
    @GetMapping("/{videoId}")
    public R<VideoDetailDTO> getVideoDetail(@PathVariable Long videoId) {
        Long userId = RequestHolder.get().getUid();
        VideoDetailDTO detail = videoService.getVideoDetail(videoId, userId);
        return R.ok(detail);
    }
    
    /**
     * 获取视频列表
     * @param categoryId 分类ID
     * @param subCategoryId 子分类ID
     * @param page 页码
     * @param size 每页大小
     * @return 视频列表
     */
    @Operation(
        summary = "获取视频列表",
        description = "分页获取视频列表，支持按分类和子分类筛选",
        responses = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "400", description = "参数错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
        }
    )
    @GetMapping
    public R<List<VideoListDTO>> getVideoList(Long categoryId, Long subCategoryId, Integer page, Integer size) {
        List<VideoListDTO> videos = videoService.getVideoList(categoryId, subCategoryId, page, size);
        return R.ok(videos);
    }
    
    /**
     * 搜索视频
     * @param keyword 关键词
     * @param page 页码
     * @param size 每页大小
     * @return 视频列表
     */
    @Operation(
        summary = "搜索视频",
        description = "根据关键词搜索视频，支持标题和描述内容搜索",
        responses = {
            @ApiResponse(responseCode = "200", description = "搜索成功"),
            @ApiResponse(responseCode = "400", description = "参数错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
        }
    )
    @GetMapping("/search")
    public R<List<VideoListDTO>> searchVideos(String keyword, Integer page, Integer size) {
        List<VideoListDTO> videos = videoService.searchVideos(keyword, page, size);
        return R.ok(videos);
    }
    
    /**
     * 获取视频流
     * @param videoId 视频ID
     * @return 视频流URL
     */
    @GetMapping("/{videoId}/stream")
    public R<String> getVideoStream(@PathVariable Long videoId) {
        String streamUrl = videoService.getVideoStreamUrl(videoId);
        return R.ok(streamUrl);
    }
} 